/*
 * YearFractionDateFormatter.java
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package vegadataeditor.output.dateFormaters;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author lawinslow
 */
public class YearFractionDateFormat implements IDateFormatter{
    GregorianCalendar cal = new GregorianCalendar();

    public String format(Date d) {
        cal.setTime(d);
        if(cal.isLeapYear(cal.get(Calendar.YEAR))){
            return Double.toString(
                    1.0*cal.get(Calendar.YEAR)+cal.get(Calendar.DAY_OF_YEAR)/366.0
                    +cal.get(Calendar.HOUR_OF_DAY)/(24.0*366)+cal.get(Calendar.MINUTE)/(60.0*24*366)
                    );
        }else{
            return Double.toString(
                    1.0*cal.get(Calendar.YEAR)+cal.get(Calendar.DAY_OF_YEAR)/365.0
                    +cal.get(Calendar.HOUR_OF_DAY)/(24.0*365)+cal.get(Calendar.MINUTE)/(60.0*24*365)
                    );
        }
    }

    public String getHeader() {
        return "YearFrac";
    }


}
